package furnitures 
{
	import simple3D.core.DisplayObject3D;
	import simple3D.core.ShapeData;
	import simple3D.core.ShapeFactory;
	import simple3D.core.Surface3D;
	import simple3D.materials.ColorMaterial;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class Foot3D extends DisplayObject3D
	{
		private var _footWidth:Number;
		private var _footHeight:Number;
		private static const DEFAULT_COLOR:uint = 0x0080C0;
		
		public function Foot3D(footW:Number, footH:Number) 
		{
			_footWidth = footW;
			_footHeight = footH;
			initFoot();
		}
		
		private function initFoot():void 
		{
			var _edgePoints:Array = makeRectangleShape(_footWidth, _footWidth);
			var _surfaces:Vector.<Surface3D> = new Vector.<Surface3D>();
			var points:Vector.<Number>;
			var pointsXY:Array;
			var prevXY:Array;
			var surf:Surface3D;
			var shapeData:ShapeData;
			
			// start i=1 - build line with preview point
			prevXY = _edgePoints[0];
			
			for (var i:int = 1; i < _edgePoints.length; i++ )
			{
				
				pointsXY = _edgePoints[i];
				
				if (pointsXY.length == 2)
				{
					points = new Vector.<Number>();
					points.push(prevXY[0],prevXY[1],pointsXY[0],pointsXY[1]);
					shapeData = ShapeFactory.getPlaneDataXY(points, _footHeight);
					surf = new Surface3D(shapeData.points, shapeData.indices, shapeData.uvt, 
											new ColorMaterial(DEFAULT_COLOR));
					surf.shapeWidth = calcLength(prevXY[0], prevXY[1], pointsXY[0], pointsXY[1]);
					surf.index = i;
					prevXY = _edgePoints[i];
				}else if (pointsXY.length > 2)
				{
					// add curves
					/*points = new Vector.<Number>();
					var k:int = 0;
					// make points arr : 3 pair points
					var startXY:Array = [prevXY[0], prevXY[1]];
					while (k < pointsXY.length)
					{
						// pair points {x,y} start, control, end
						points.push(prevXY[0], prevXY[1], pointsXY[k+2], pointsXY[k+3], pointsXY[k], pointsXY[k+1]);
						prevXY = [pointsXY[k],  pointsXY[k + 1]];
						k += 4;
					}
					k -= 4; // setup current k
					// make data
					shapeData = ShapeFactory.getBezierDataMulti(points, _footHeight);
					surf = new Surface3D(shapeData.points, shapeData.indices, shapeData.uvt, 
											new ColorMaterial(DEFAULT_COLOR));
					surf.shapeWidth = calcLength(startXY[0], startXY[1], pointsXY[k], pointsXY[k+1]);
					surf.index = i;*/
				}else
				{
					throw Error("Wrong shape data");
				}
				if (surf != null)
				{
					_surfaces.push(surf);
				}
				surf = null;
			}
			
			for (i = 0; i < _surfaces.length; i++ )
			{
				//_surfaces[i].position.x += _footWidth * 0.5;
				//_surfaces[i].position.z += _footWidth * 0.5;
				addChild(_surfaces[i]);
				
				_surfaces[i].position.x -= _footWidth * 0.5;
				_surfaces[i].position.z -= _footWidth * 0.5;
			}
			
			centrPoint.x = _footWidth * 0.5;
			centrPoint.z = _footWidth * 0.5;
			centrPoint.y = _footHeight * 0.5;
			
			// test centr point
			/*_edgePoints = makeRectangleShape(_footWidth, _footWidth);
			
			shapeData = ShapeFactory.getPlaneDataXY(points, _footHeight)
			surf = new Surface3D(shapeData.points, shapeData.indices, shapeData.uvt, 
											new ColorMaterial(0xFF0000));
			surf.position = centrPoint.clone();
			addChild(surf);*/
		}
			
		
		private function makeRectangleShape(rectWidth:Number, rectHeight:Number, 
											startX:Number = 0,
											startY:Number = 0 ):Array
		{
			var points:Array = [];
			points.push([startX, startY]);
			points.push([startX+ rectWidth, startY]);
			points.push([startX + rectWidth, startY + rectHeight]);
			points.push([startX, startY + rectHeight]);
			points.push([startX, startY]);
			return points;
		}
		
		private function calcLength(x1:Number, y1:Number, x2:Number, y2:Number):Number
		{
			return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1)*(y2 - y1));
		}
		
	}

}